import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# 设置服务器所需信息
# 163邮箱服务器地址
mail_host = 'smtp.qq.com'
# 163用户名
mail_user = '549144697@qq.com'
# 密码(部分邮箱为授权码)
mail_pass = 'mmzrywokizitbdae'
# 邮件发送方邮箱地址
sender = '549144697@qq.com'
msg_from = '549144697@qq.com'
# 邮件接受方邮箱地址，注意需要[]包裹，这意味着你可以写多个邮件地址群发
receivers = ['549144697@qq.com','656832681@qq.com','530431698@qq.com']


def send_email(content,subject = None, file_addr=None):
    # MIMEMultipart类可以放任何内容
    msg = MIMEMultipart()
    # 把内容加进去
    msg.attach(MIMEText(content, 'plain', 'utf-8'))
    # 设置邮件主题
    if subject:
        msg['Subject'] = subject
    else:
        msg['Subject'] = "这个是邮件主题"

    # 发送方信息
    msg['From'] = msg_from
    if  file_addr:
        # 添加附件
        att1 = MIMEText(open(file_addr, 'rb').read(), 'base64', 'utf-8')  # 打开附件
        att1['Content-Type'] = 'application/octet-stream'  # 设置类型是流媒体格式
        att1['Content-Disposition'] = 'attachment;filename=runninLog.log'  # 设置描述信息

        msg.attach(att1)
    # 登录并发送邮件
    s = smtplib.SMTP_SSL("smtp.qq.com", 465)
    # 登录到服务器
    s.login(mail_user, mail_pass)
    # 发送
    s.sendmail(
        sender, receivers, msg.as_string())
    # 退出
    s.quit()


if __name__ == '__main__':
    # 设置email信息
    # 邮件内容设置

    # 设置邮件内容

    content = "发送附件测试"
    # file_addr = r'C:\Users\zhangcan\PycharmProjects\make_money_together\strategies\macdtest.csv'

    send_email(content)
    print('mail send success')
